package com.qigao.mall.web.controller.app.merchant;

import com.alibaba.fastjson.JSONObject;
import com.qigao.mall.api.DataStoreService;
import com.qigao.mall.api.OpenShopService;
import com.qigao.mall.api.ShopInfoService;
import com.qigao.mall.commons.dto.OpenShopDto;
import com.qigao.mall.commons.dto.ShopInfoCreateDto;
import com.qigao.mall.commons.dto.ShopInfoDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.exceptions.BusinessException;
import com.qigao.mall.commons.po.UserInfo;
import com.qigao.mall.jdbc.model.SysUser;
import com.qigao.mall.web.config.QiGaoProperties;
import com.qigao.mall.web.controller.AbstractShopController;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.app.MerShopCreateVo;
import com.qigao.mall.web.vo.app.MerShopModiVo;
import com.qigao.mall.web.vo.app.OpenShopVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/app/merchant/shop/")
@Api(tags = "App商户店铺管理")
public class MerchantShopController extends AbstractShopController{

    private static final Logger logger = LoggerFactory.getLogger(MerchantShopController.class);
    @Resource
    private ShopInfoService shopInfoService;
    @Resource
    private OpenShopService openShopService;
    @Resource
    private QiGaoProperties qiGaoProperties;
    @Resource
    private DataStoreService dataStoreService;
    //自动开店，免费用15天
    @PostMapping("/autoOpenShop")
    @ApiOperation(value = "自动开通店铺", notes = "自动开通店铺")
    public Result autoOpenShop(@RequestBody OpenShopVo openShopVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(openShopVo);
        try{
            String  sysUserStr=dataStoreService.findOneByMerNo(qiGaoProperties.getMerNo());
            if(sysUserStr==null){
                logger.info("需要在商城用户端先登录才行....");
                return Result.newError(ResultCode.COND_NOT_ALLOWED);
            }
            OpenShopDto openShopDto =new OpenShopDto();
            BeanUtils.copyProperties(openShopVo, openShopDto);
            openShopDto.setMerNo(qiGaoProperties.getMerNo());
            openShopDto.setShopNo(qiGaoProperties.getShopNo());
            //获取用户端登录的信息
            SysUser sysUser= JSONObject.parseObject(sysUserStr,SysUser.class);
            openShopDto.setAccessToken(sysUser.getResv1());
            openShopDto.setOperNo(RequestUtils.getCurrentUserNo(request));
            return openShopService.autoOpenShop(openShopDto);
        }catch (BusinessException be) {
            logger.error("开通店铺发生业务异常:{}",be);
            return Result.newError(be.getCode(),be.getMsg());
        }catch (Exception e){
            logger.error("开通店铺发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //在线开通店铺
    @PostMapping("/openShop")
    @ApiOperation(value = "在线开通店铺", notes = "在线开通店铺")
    public Result openShop(@RequestBody OpenShopVo openShopVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(openShopVo);
        try{
            OpenShopDto openShopDto =new OpenShopDto();
            BeanUtils.copyProperties(openShopVo, openShopDto);
            openShopDto.setSysMerNo(qiGaoProperties.getMerNo());
            openShopDto.setSysShopNo(qiGaoProperties.getShopNo());
            openShopDto.setOperNo(RequestUtils.getCurrentUserNo(request));
            return openShopService.openShop(openShopDto);
        }catch (BusinessException be) {
            logger.error("开通店铺发生业务异常:{}",be);
            return Result.newError(be.getCode(),be.getMsg());
        }catch (Exception e){
            logger.error("开通店铺发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //获取店铺小程序二维码
    @GetMapping("/getWeiChatQrCode/{shopNo}")
    @ApiOperation(value = "按照店铺号获取店铺二维码", notes = "按照店铺号获取店铺二维码")
    public Result getWeiChatQrCode(@PathVariable("shopNo")String shopNo,HttpServletRequest request){
        try{
            UserInfo userInfo=RequestUtils.getCurrentUser(request);
            return openShopService.getWeiChatQrCode(shopNo,userInfo.getResv1());
        }catch (Exception e){
            logger.error("按照店铺号获取店铺二维码发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //按照店铺名称查询店铺信息
    @GetMapping("/findByShopName/{shopName}")
    @ApiOperation(value = "按照店铺名称查询店铺信息", notes = "按照店铺名称查询店铺信息")
    public Result findByShopName(@PathVariable("shopName")String shopName){
        try{
            ShopInfoDto shopInfoDto = shopInfoService.findByShopName(shopName);
            if(shopInfoDto ==null ){
                return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
            }
            return Result.newSuccess(shopInfoDto);
        }catch (Exception e){
            logger.error("按照商户号查询店铺信息发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    //创建店铺
    @PostMapping("/create")
    @ApiOperation(value = "创建店铺", notes = "创建店铺")
    public Result create(@RequestBody MerShopCreateVo merShopCreateVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(merShopCreateVo);
        try{
            ShopInfoCreateDto shopInfoCreateDto =new ShopInfoCreateDto();
            BeanUtils.copyProperties(merShopCreateVo, shopInfoCreateDto);
            shopInfoCreateDto.setOperNo(RequestUtils.getCurrentUserNo(request));
            shopInfoService.create(shopInfoCreateDto);
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("开通店铺发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改店铺", notes = "修改店铺")
    public Result update(@RequestBody MerShopModiVo merShopModiVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(merShopModiVo);
        try{
            ShopInfoCreateDto shopInfoCreateDto =new ShopInfoCreateDto();
            BeanUtils.copyProperties(merShopModiVo, shopInfoCreateDto);
            shopInfoCreateDto.setId(Long.valueOf(merShopModiVo.getId()));
            shopInfoCreateDto.setOperNo(RequestUtils.getCurrentUserNo(request));
            shopInfoService.update(shopInfoCreateDto);
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("修改店铺发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    //2.启用禁用店铺
    @PostMapping("/enableDisable/{id}")
    @ApiOperation(value = "启用禁用店铺", notes = "启用禁用店铺")
    public Result enableDisable(@PathVariable("id")Long id, HttpServletRequest request){
        try{
            shopInfoService.enableDisable(id,RequestUtils.getCurrentUserNo(request));
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("启用禁用店铺发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    //3.按照商户号查询店铺列表
    @GetMapping("/findListByMerNo/{merNo}")
    @ApiOperation(value = "按照商户号查询店铺列表", notes = "按照商户号查询店铺列表")
    public Result findListByMerNo(@PathVariable("merNo")String merNo){
        try{
            List<ShopInfoDto> shopInfoDtos = shopInfoService.findListByMerNo(merNo);
            if(shopInfoDtos ==null || shopInfoDtos.size()<=0){
                return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
            }
            return Result.newSuccess(shopInfoDtos);
        }catch (Exception e){
            logger.error("按照商户号查询店铺列表发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

}
